<?php
/**
 * Leek - Zend Framework Components
 *
 * @category   Leek
 * @package    Leek_Controller
 * @subpackage Leek_Controller_Action_Helper
 * @author     Jani Hartikainen <jani@codeutopia.net>
 * @author     Chris Jones <leeked@gmail.com>
 * @see        http://codeutopia.net/code/library/CU/Controller/Action/Helper/History.php
 * @link       http://code.google.com/p/leek-zf-components/
 * @copyright  2008 Jani Hartikainen <www.codeutopia.net>
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: History.php 52 2009-06-02 22:11:31Z leeked $
 */

/**
 * Action Helper for tracking a user's browsing history
 *
 * @uses       Zend_Controller_Action_Helper_Abstract
 * @category   Leek
 * @package    Leek_Controller
 * @subpackage Leek_Controller_Action_Helper
 * @author     Jani Hartikainen <jani@codeutopia.net>
 * @author     Chris Jones <leeked@gmail.com>
 * @see        http://codeutopia.net/code/library/CU/Controller/Action/Helper/History.php
 * @link       http://code.google.com/p/leek-zf-components/
 * @copyright  2008 Jani Hartikainen <www.codeutopia.net>
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
class Leek_Controller_Action_Helper_History extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @var Zend_Session_Namespace
     */
    private $_namespace;

    /**
     * How many history URLs to track?
     *
     * @var int
     */
    private $_trackAmount = 2;

    /**
     * Constructor
     *
     * @param int $trackAmount [optional] How many history URLs to track
     * @return void
     */
    public function __construct($trackAmount = null)
    {
        if ($trackAmount === null) {
            $trackAmount = $this->getTrackAmount();
        }

        $this->setTrackAmount($trackAmount);
        $this->_initSession();
    }

    /**
     * Initialize the history from session
     *
     * @return void
     */
    private function _initSession()
    {
        $this->_namespace = new Zend_Session_Namespace('Leek_Controller_Action_Helper_History');

        if (!is_array($this->_namespace->history)) {

            $this->_namespace->history = array();

            if (!empty($_SERVER['HTTP_REFERER'])) {
                array_unshift($this->_namespace->history, $_SERVER['HTTP_REFERER']);
            } else {
                array_splice($this->_namespace->history, $this->getTrackAmount());
            }

        }
    }

    /**
     * Defined by Zend_Controller_Action_Helper_Abstract
     *
     * @return void
     */
    public function preDispatch()
    {
        $urlHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
        array_unshift($this->_namespace->history, $urlHelper->url());
    }


    /**
     * Set how many history URLs to track
     *
     * @param int $trackAmount
     * @return Leek_Controller_Action_Helper_History
     */
    public function setTrackAmount($trackAmount)
    {
        $this->_trackAmount = (int) $trackAmount;
        return $this;
    }

    /**
     * Get total URLs to track
     *
     * @return int
     */
    public function getTrackAmount()
    {
        return $this->_trackAmount;
    }

    /**
     * Redirects the browser back in history
     *
     * @param int $amount How many URLs to go back
     */
    public function goBack($amount = 1)
    {
        Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')
                                           ->setPrependBase(false)
                                           ->gotoUrl($this->_namespace->history[$amount]);
    }

    /**
     * Returns an URL from history
     *
     * @param int $amount How many URLs to go back
     * @return string
     */
    public function getPreviousUrl($amount = 1)
    {
        return $this->_namespace->history[$amount];
    }

    /**
     * Return all previous URLs
     *
     * @return array
     */
    public function getArray()
    {
        return $this->_namespace->history;
    }
}
